package com.edgar.download;

import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;

import com.edgar.download.database.Downloads;

import java.io.File;

/**
 * Created by dell on 2015/9/23.
 */
public class Download implements Downloads.DownloadImpl {

    private long mIds;
    private long mContentId;
    private String mDownloadUrl;
    private String mTitle;  //显示标题
    private volatile short mStatus;
    private String mFilePath;
    private int mDownloadProgress;
    private long mTotalSize;
    private long mDownloadSize;  //已下载大小
    private long mDownloadTime;
    private String mContentType;
    private int mErrorStatusCode;
    private String mErrorStatusMessage;
    private volatile boolean mIsCancel = false;

    /**
     * 创建一个下载对象
     * @param url
     * @param filePath 下载路径,为空则使用默认的目录
     * @param title
     * @return
     */
    public static Download createDownload(String url,String filePath,String title){
        if(TextUtils.isEmpty(url)){
            throw new NullPointerException("URL can not is null.");
        }
        if(!url.startsWith("http") && !url.startsWith("https")){
            throw new IllegalArgumentException("Invalid URL:"+url);
        }
        if(TextUtils.isEmpty(filePath)){
            filePath = Constant.DEFAULT_DOWNLOAD_DIR + File.separator + title;
        }
        Download download = new Download();
        download.setTitle(title);
        download.setDownloadUrl(url);
        download.setFilePath(filePath);
        return download;
    }

    public static Download createDownload(Cursor cursor){
        Download download = new Download();
        download.mIds = cursor.getInt(cursor.getColumnIndex(_ID));
        download.mContentId = cursor.getLong(cursor.getColumnIndex(CONTENT_ID));
        download.mTitle = cursor.getString(cursor.getColumnIndex(NAME));
        download.mFilePath = cursor.getString(cursor.getColumnIndex(SAVE_PATH));
        download.mDownloadProgress = cursor.getInt(cursor.getColumnIndex(PROGRESS));
        download.mDownloadTime = cursor.getLong(cursor.getColumnIndex(DATE));
        download.mDownloadUrl = cursor.getString(cursor.getColumnIndex(DOWNLOAD_URL));
        download.mTotalSize = cursor.getLong(cursor.getColumnIndex(SIZE));
        download.mDownloadSize = cursor.getLong(cursor.getColumnIndex(CURRENT_SIZE));
        download.mContentType = cursor.getString(cursor.getColumnIndex(CONTENT_TYPE));
        download.mErrorStatusCode = cursor.getInt(cursor.getColumnIndex(ERROR_STATUS));
        download.mErrorStatusMessage = cursor.getString(cursor.getColumnIndex(ERROR_MESSAGE));
        short status = cursor.getShort(cursor.getColumnIndex(STATUS));
        if(status == Constant.DOWNLOADING){
            status = Constant.PAUSE;
        }
        download.mStatus = status;
        return download;
    }

    public ContentValues toContentValue(){
        ContentValues contentValues = new ContentValues();
        contentValues.put(NAME, mTitle);
        contentValues.put(SAVE_PATH, mFilePath);
        contentValues.put(PROGRESS,mDownloadProgress);
        contentValues.put(DATE,mDownloadTime);
        contentValues.put(DOWNLOAD_URL,mDownloadUrl);
        contentValues.put(SIZE, mTotalSize);
        contentValues.put(STATUS,mStatus);
        contentValues.put(CURRENT_SIZE,mDownloadSize);
        contentValues.put(CONTENT_TYPE,mContentType);
        contentValues.put(ERROR_STATUS,mErrorStatusCode);
        contentValues.put(ERROR_MESSAGE,mErrorStatusMessage);
        contentValues.put(CONTENT_ID,mContentId);
        return contentValues;
    }

    public ContentValues toUpdateContentValue(){
        ContentValues contentValues = new ContentValues();
        contentValues.put(Downloads.DownloadImpl.STATUS,mStatus);
        contentValues.put(ERROR_STATUS,mErrorStatusCode);
        contentValues.put(ERROR_MESSAGE,mErrorStatusMessage);
        contentValues.put(DOWNLOAD_URL,mDownloadUrl);
        contentValues.put(CURRENT_SIZE,mDownloadSize);
        contentValues.put(CONTENT_TYPE,mContentType);
        contentValues.put(SIZE,mTotalSize);
        contentValues.put(PROGRESS,mDownloadProgress);
        return contentValues;
    }

    public void setId(long id){
        this.mIds = id;
    }

    public void setDownloadSize(long range){
        this.mDownloadSize = range;
    }

    public long getDownloadSize(){
        return mDownloadSize;
    }

    public void setSize(long size){
        this.mTotalSize = size;
    }

    public long getSize(){
        return mTotalSize;
    }

    public String getDownloadUrl(){
        return mDownloadUrl;
    }

    public void setDownloadUrl(String url){
        this.mDownloadUrl = url;
    }

    public void setFilePath(String downloadPath){
        this.mFilePath = downloadPath;
    }

    public String getFilePath(){
        return mFilePath;
    }

    public long getId(){
        return mIds;
    }

    public void setStatus(short status){
        this.mStatus = status;
    }

    public short getStatus(){
        return mStatus;
    }

    public void setProgress(int progress){
        this.mDownloadProgress = progress;
    }

    public int getProgress(){
        return mDownloadProgress;
    }

    public void setTitle(String name){
        this.mTitle = name;
    }

    public String getTitle(){
        return mTitle;
    }

    public void setDownloadTime(long downloadTime){
        this.mDownloadTime = downloadTime;
    }

    public long getDownloadTime(){
       return mDownloadTime;
    }

    public void setCancel(boolean isCancel){
        this.mIsCancel = isCancel;
    }

    public boolean isCancel(){
        return this.mIsCancel;
    }

    public int getErrorStatusCode() {
        return mErrorStatusCode;
    }

    public void setErrorStatusCode(int errorStatusCode) {
        this.mErrorStatusCode = errorStatusCode;
    }

    public String getErrorStatusMessage() {
        return mErrorStatusMessage;
    }

    public void setErrorStatusMessage(String errorStatusMessage) {
        this.mErrorStatusMessage = errorStatusMessage;
    }

    public void setContentId(long contentId){
        this.mContentId = contentId;
    }

    public long getContentId(){
        return mContentId;
    }

    public void setContentType(String contentType){
        this.mContentType = contentType;
    }

    public String getContentType(){
       return mContentType;
    }

    @Override
    public boolean equals(Object o) {
        if(o == null) return false;
        Download download = (Download)o;
        return download.getDownloadUrl().equals(mDownloadUrl);
    }
}